package persistence;

import beans.Atualizacao;
import beans.Usuario;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;



public class DAOUsuario {
    private EntityManager em;

    public DAOUsuario(EntityManager em) {
        this.em = em;
    }
    public String criptografaSenha (String senha) throws NoSuchAlgorithmException {  
        MessageDigest md = MessageDigest.getInstance("MD5");  
        BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));  
        String s = hash.toString(16);  
        if (s.length() %2 != 0)  
            s = "0" + s;  
        return s;  
    }
    
    public Usuario BuscaLoginSenha(String email){
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();
        Root<Usuario> rUser = cq.from(Usuario.class);
        cq.select(rUser);
        cq.where(cb.equal(rUser.get("login"), email));
        
        Query q = (Query) em.createQuery(cq);
        
        Usuario user = null;
        try {
            user = (Usuario) q.getSingleResult();
        } catch (NoResultException e) {
            
        }
        
        return user;
    }
    
    public Usuario buscaid(long id){
        em.getTransaction().begin();
        return em.find(Usuario.class, id);
    }
    
    public List<Atualizacao> buscaAts(Usuario userPublica){
        
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();
        Root<Atualizacao> rAt = cq.from(Atualizacao.class);
        cq.select(rAt);
        cq.where(cb.equal(rAt.get("userPublica").get("id"), userPublica.getId())).orderBy(cb.asc(rAt.get("dataAtualiza")));
        Query q = (Query) em.createQuery(cq);
        return q.getResultList();
        
    }
    
    public void atualizacoes(Usuario user, Atualizacao at){
        for(Usuario u : user.getAmigos()){
            List<Atualizacao> ats = u.getAtualizacoesPartc();
            ats.add(at);
            u.setAtualizacoesPartc(ats);
            DAO dao = new DAO(em, Usuario.class);
            dao.atualiza(u);
        }
    }
    
    public List<Usuario> BuscaNome(String nome){
        
        List<Usuario> users = new ArrayList<Usuario>();
        
        System.out.println("nome--------------------------" + nome);
        
        Query q = em.createQuery("SELECT u FROM Usuario u WHERE u.nome LIKE'%" + nome + "%' or u.sobrenome like'%" + nome + "%'");
        System.out.println("SQL ================------------------__________________" + q.toString());
        
        users = q.getResultList();
        
        return users;
    }
    
}
